package xworker.swt.data;

import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.dataObject.data.StaticDataManager;
import xworker.dataObject.DataObject;
import xworker.dataObject.DataObjectList;
import xworker.lang.VariableDesc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SStaticDataManager {
    //xworker.swt.data.StaticDataManager/@actions/@create
    public static void create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        List<Thing> dataManagers = self.doAction("getStaticDataManagers", actionContext);
        List<String> variables = self.doAction("getVariables", actionContext);
        Map<String, String> context = new HashMap<>();
        if(dataManagers != null && variables != null){
            for(Thing dataManager : dataManagers){
                StaticDataManager staticDataManager = StaticDataManager.getStaticDataManager(dataManager);
                for(String var : variables){
                    String[] vars = var.split("[>]");
                    String srcName = vars[0].trim();
                    String tagName = vars.length > 1 ? vars[1].trim() : srcName;

                    if(context.get(srcName) == null) {
                        actionContext.g().put(tagName, staticDataManager.get(srcName));
                        context.put(srcName, srcName);
                    }
                }
            }
        }
    }

    //xworker.swt.data.StaticDataManager/@VariablesDescs/@Variables/@actions/@create
    public static List<VariableDesc> createVarDescs(ActionContext actionContext){
        Thing thing = actionContext.getObject("thing");

        List<VariableDesc> descs = new ArrayList<>();
        List<Thing> dataManagers = thing.doAction("getStaticDataManagers", actionContext);
        List<String> variables = thing.doAction("getVariables", actionContext);
        Map<String, String> context = new HashMap<>();

        if(dataManagers != null && variables != null){
            for(Thing dataManager : dataManagers){
                StaticDataManager staticDataManager = StaticDataManager.getStaticDataManager(dataManager);
                for(String var : variables){
                    String[] vars = var.split("[>]");
                    String srcName = vars[0].trim();
                    String tagName = vars.length > 1 ? vars[1].trim() : srcName;

                    if(context.get(srcName) == null) {
                        Object obj = staticDataManager.get(srcName);
                        if(obj instanceof Thing){
                            descs.add(new VariableDesc(tagName, VariableDesc.THING, obj.getClass().getName(), (Thing) obj, VariableDesc.SCOPE_GLOBAL));
                        }else if(obj instanceof DataObject){
                            Thing dataThing = ((DataObject) obj).getMetadata().getDescriptor();
                            descs.add(new VariableDesc(tagName, VariableDesc.DATAOBJECT, obj.getClass().getName(), dataThing, VariableDesc.SCOPE_GLOBAL));
                        }else if(obj instanceof DataObjectList){
                            Thing dataThing = ((DataObjectList) obj).getDescriptor();
                            descs.add(new VariableDesc(tagName, VariableDesc.DATAOBJECTLIST, obj.getClass().getName(), dataThing, VariableDesc.SCOPE_GLOBAL));
                        }else{
                            descs.add(new VariableDesc(tagName, VariableDesc.OBJECT, obj.getClass().getName(), null, VariableDesc.SCOPE_GLOBAL));
                        }

                        context.put(srcName, srcName);
                    }
                }
            }
        }

        return descs;
    }
}
